package com.easyweb.session;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.framework.EntityHome;
import org.jboss.seam.international.StatusMessage;
import org.jboss.seam.international.StatusMessages;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import com.easyweb.entity.Bayi;
import com.easyweb.entity.BayiGps;
import com.easyweb.entity.Ilce;
import com.easyweb.entity.Iller;
import com.easyweb.entity.Semtler;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import org.jboss.seam.persistence.PersistenceProvider;

@Name("bayiAdres")
public class BayiAdres extends EntityHome<Bayi> {

	@In(required = false)
	FacesContext facesContext;

	@In
	EntityManager entityManager;

	private Iller iller;
	private Ilce ilce;
	private Semtler semt;
	private List<Bayi> bayilerList = new ArrayList<Bayi>();
	private List<Semtler> semtlerList = new ArrayList<Semtler>();;
	private List<Ilce> ilcelerList = new ArrayList<Ilce>();
	private List<SelectItem> kullaniciList = null;

	private String enlem = null, boylam = null, tip = null;
	private String latitude, longitude, type;
	private String kullaniciKodu;

	@Override
	protected Bayi createInstance() {
		Bayi bayi = new Bayi();
		return bayi;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		if (latitude instanceof String && !(enlem instanceof String))
			enlem = latitude;
		if (longitude instanceof String && !(boylam instanceof String))
			boylam = longitude;
		if (type instanceof String && !(tip instanceof String))
			tip = type;
	}

	public boolean isWired() {
		return true;
	}

	public Bayi getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	public void bayiCodeListener(ActionEvent event) {
		instance = (Bayi) comboBul("bayiAdresForm:bayiDeco:bayilerId")
				.getValue();

		/* ilçesi hazır ise onu da set eder */
		HtmlSelectOneMenu comboIlce = comboBul("bayiAdresForm:ilceDeco:ilcelerId");
		comboIlce.setValue(instance.getIlce());

		/* semt i de doldurur */
		/*
		 * HtmlSelectOneMenu comboSemt =
		 * comboBul("bayiAdresForm:semtDeco:semtlerId"); Semtler tempSemt = new
		 * Semtler(); tempSemt = entityManager .find(Semtler.class,
		 * instance.getSemt().getId()); comboSemt.setValue(tempSemt);
		 */
	}

	public void kullaniciCodeAl() {
		if (kullaniciKodu != null && kullaniciKodu instanceof String) {
			bayiListesiDoldur();
		}
	}

	public void bayiCodeAl() {
		iller = instance.getIl();
		ilceListesiDoldur();
		ilce = instance.getIlce();
		semtlerListesiDoldur();
		semt = instance.getSemt();
	}

	public void IlCodeListener(ActionEvent event) {
		iller = (Iller) comboBul("bayiAdresForm:illerDeco:IlID").getValue();
		temizle();
		instance.setIl(iller);
		ilceListesiDoldur();
	}

	public void ilCodeAl() {
		// iller = (Iller) comboBul("bayiAdresForm:illerDeco:IlID").getValue();
		temizle();
		instance.setIl(iller);
		ilceListesiDoldur();
	}

	public void ilceListesiDoldur() {

		if (iller != null && iller.getId() instanceof Long) {
			ilcelerList = entityManager.createNamedQuery(
					"ilcelerTumKayitlarIleGore").setParameter("p1",
					iller.getId()).getResultList();
		}
	}

	public void bayiListesiDoldur() {
		if (kullaniciKodu != null && kullaniciKodu instanceof String) {
			bayilerList = entityManager.createNamedQuery(
					"bayilerTumKayitlarKullaniciyaGore").setParameter("p1",
					kullaniciKodu).getResultList();
		}
	}

	@Override
	public String persist() {
		/* kontroller */
		if (ilce == null || instance == null || ilce == null || semt == null
				|| !(instance.getPostaKodu() instanceof String)
				|| !(instance.getCadde() instanceof String)
				|| !(instance.getSokak() instanceof String)
				|| !(instance.getBinaNo() instanceof String)
				|| !(instance.getBinaAdi() instanceof String)
				|| !(instance.getKatNo() instanceof String)
				|| !(instance.getDaireNo() instanceof String)) {

			StatusMessages.instance().add("#{messages.errTumAlanlarZorunlu}");
			return "false";
		}

		if (enlem instanceof String) {
			BayiGps bayiGps = new BayiGps();
			bayiGps.setBayi(getInstance());
			bayiGps.setLatitude(enlem);
			bayiGps.setLongitude(boylam);
			bayiGps.setType(tip);

			/**/
			if (!(getInstance().getTicariUnvan() instanceof String))
				getInstance().setTicariUnvan(getInstance().getUnvan());

			instance.setSemt(semt);
			getEntityManager().persist(getInstance());
			getEntityManager().flush();
			assignId(PersistenceProvider.instance().getId(getInstance(),
					getEntityManager()));
			createdMessage();
			raiseAfterTransactionSuccessEvent();
			getEntityManager().persist(bayiGps);
			getEntityManager().flush();
			return "persisted";
		} else {
			StatusMessages.instance().add("#{messages.errGpsVerisiAlınamadi}");
			return "false";
		}

	}

	private void temizle() {
		instance.setPostaKodu(null);
		instance.setCadde(null);
		instance.setSokak(null);
		instance.setBinaAdi(null);
		instance.setBinaNo(null);
		instance.setKatNo(null);
		instance.setDaireNo(null);
	}

	public void IlceCodeListener(ActionEvent event) {
		instance.setIlce((Ilce) comboBul("bayiAdresForm:ilceDeco:ilcelerId")
				.getValue());
		semtlerListesiDoldur();
	}

	public void ilceCodeAl() {
		instance.setIlce(ilce);
		semtlerListesiDoldur();
	}

	public void semtlerListesiDoldur() {
		if (instance.getIlce() != null
				&& instance.getIlce().getId() instanceof Long) {
			semtlerList = entityManager.createNamedQuery(
					"semtlerTumKayitlarIleGore").setParameter("p1",
					instance.getIlce().getId()).getResultList();
		}
	}

	public HtmlSelectOneMenu comboBul(String comboAdi) {
		return (HtmlSelectOneMenu) facesContext.getViewRoot().findComponent(
				comboAdi);

	}

	public void fillKullaniciList() {
		kullaniciList = new ArrayList<SelectItem>();
		List<String> kullaniciListDummy = new ArrayList<String>();
		kullaniciListDummy = entityManager.createNamedQuery(
				"bayilerKullaniciler").getResultList();
		for (String string : kullaniciListDummy) {
			kullaniciList.add(new SelectItem(string, string));
		}
	}

	public Iller getIller() {
		return iller;
	}

	public void setIller(Iller iller) {
		this.iller = iller;
	}

	public List<Bayi> getBayilerList() {
		return bayilerList;
	}

	public void setBayilerList(List<Bayi> bayilerList) {
		this.bayilerList = bayilerList;
	}

	public Semtler getSemt() {
		return semt;
	}

	public void setSemt(Semtler semt) {
		this.semt = semt;
	}

	public List<Semtler> getSemtlerList() {
		return semtlerList;
	}

	public void setSemtlerList(List<Semtler> semtlerList) {
		this.semtlerList = semtlerList;
	}

	public List<Ilce> getIlcelerList() {
		return ilcelerList;
	}

	public void setIlcelerList(List<Ilce> ilcelerList) {
		this.ilcelerList = ilcelerList;
	}

	public Ilce getIlce() {
		return ilce;
	}

	public void setIlce(Ilce ilce) {
		this.ilce = ilce;
	}

	public String getEnlem() {
		return enlem;
	}

	public String getBoylam() {
		return boylam;
	}

	public void setEnlem(String enlem) {
		this.enlem = enlem;
	}

	public void setBoylam(String boylam) {
		this.boylam = boylam;
	}

	public String getLatitude() {
		return latitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getTip() {
		return tip;
	}

	public String getType() {
		return type;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<SelectItem> getKullaniciList() {
		if (kullaniciList == null)
			fillKullaniciList();
		return kullaniciList;
	}

	public void setKullaniciList(List<SelectItem> kullaniciList) {
		this.kullaniciList = kullaniciList;
	}

	public String getKullaniciKodu() {
		return kullaniciKodu;
	}

	public void setKullaniciKodu(String kullaniciKodu) {
		this.kullaniciKodu = kullaniciKodu;
	}

}
